package com.southsmart.springboot.smartdoc.controller;


import com.southsmart.springboot.smartdoc.api.Result;
import com.southsmart.springboot.smartdoc.dto.AdministrativeDivisionDTO;
import com.southsmart.springboot.smartdoc.dto.AdministrativeDivisionTreeDTO;
import com.southsmart.springboot.smartdoc.dto.AdministrativeLevelDTO;
import com.southsmart.springboot.smartdoc.service.IAdministrativeDivisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 * AdministrativeDivisionController 控制层
 *
 * @author : CaoLongMin
 * @since 2021-04-20
 * @version 1.0
 */
@RestController
@RequestMapping("administrativeDivision")
public class AdministrativeDivisionController {

	@Autowired
	private IAdministrativeDivisionService administrativeDivisionService;



	/**
	 * 根据主键id查询
	 *
	 * @param  id 主键id测试
	 * @return result 结果对象
	 */
	@GetMapping("/selectById")
	public Result<AdministrativeDivisionDTO> selectById(@RequestParam String id) {
		return Result.ok(administrativeDivisionService.selectById(id));
	}

	/**
	 * 批量删除
	 *
	 * @param ids 主键id，多个都好分隔
	 * @return result 结果对象
	 */
	@PostMapping(value = "/deleteBatchIds")
	public Result<Integer> deleteBatchIds(@RequestParam String[] ids) {
		List<String> list = Arrays.asList(ids);
		int i = administrativeDivisionService.deleteByIds(list);
		return Result.ok(i);
	}

	/**
	 * 新增或修改记录
	 *
	 * @param administrativeDivisionDTO 实体传输类
	 * @return result 结果对象
	 */
	@PostMapping(value ="saveOrUpdate")
	public Result saveOrUpdate(@RequestBody @Validated AdministrativeDivisionDTO administrativeDivisionDTO) {
		boolean result;
		if(StringUtils.isEmpty(administrativeDivisionDTO.getId())){
			result=administrativeDivisionService.insert(administrativeDivisionDTO);
		}else{
			result=administrativeDivisionService.update(administrativeDivisionDTO);
		}
		if(result){
			return Result.ok("操作成功");
		}else{
			return Result.failed("操作失败");
		}
	}

	/**
	 * 获取所有行政区级别列表
	 *
	 * @return result 结果对象
	 */
	@GetMapping(value ="getLevelList")
	public Result<List<AdministrativeLevelDTO>> getLevelList(){
		return Result.ok(administrativeDivisionService.getLevelList());
	}
	/**
	 * 验证行政区划代码
	 *
	 * @param code 行政区代码
	 * @return 存在返回true 不存在 false
	 */
	@GetMapping("/validAdminCode")
	public Result validAdminCode(@RequestParam String code) {
		return Result.ok(administrativeDivisionService.validAdminCode(code));
	}

	/**
	 * 根据行政区划代码 获取行政形结构数据
	 *
	 * @param code 行政区代码 不传(null/"")则为全部 传则顶级节点为该级
	 * @return 树结构数据
	 */
	@GetMapping("/getAdministrativeTreeByCode")
	public Result<List<AdministrativeDivisionTreeDTO>> getAdministrativeTreeByCode(@RequestParam(required = false) String code) {
		return Result.ok(administrativeDivisionService.getAdministrativeTreeByCode(code));
	}

	/**
	 * 根据行政区划级别 获取行政区树形结构数据
	 *
	 * @param levelType 行政区级别 不传(null/"")则为国家 传则顶级节点为该等级 0 国家；1省；2市；3区县
	 * @return 树结构数据
	 */
	@GetMapping("/getAdministrativeTreeByLevel")
	public Result<List<AdministrativeDivisionTreeDTO>> getAdministrativeTreeByLevel(@RequestParam(required = false) Integer levelType) {
		return Result.ok(administrativeDivisionService.getAdministrativeTreeByLevel(levelType));
	}

}
